﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using MvcCarConsume.Domain;
using System.Globalization;

namespace MvcCarConsume.WebUI
{
    public class AccountController : BaseController
    {
        private IAccountService accountService;
        private bool isLogFailed;

        public AccountController( IAccountService accountService )
        {
            this.accountService = accountService;
        }

        //
        // Account/Menu
        [ChildActionOnly]
        public ViewResult LogInMenu()
        {
            VMAccountMenuControl content = new VMAccountMenuControl();
            content.CurrentUserName = User.Identity.Name;
            content.IsAdmin = User.IsInRole("Admin");
            //haven't used
            content.IsLogFailed = this.isLogFailed;
            content.IsLogIn = User.Identity.IsAuthenticated;
            return View("LogMenuControl",content);
        }
        //

        [ChildActionOnly]
        public ViewResult HomeMenu()
        {
            VMAccountMenuControl content = new VMAccountMenuControl();
            content.CurrentUserName = User.Identity.Name;
            content.IsAdmin = User.IsInRole("Admin");
            //haven't used
            content.IsLogFailed = this.isLogFailed;
            content.IsLogIn = User.Identity.IsAuthenticated;
            return View("HomeMenuControl", content);
        }

        #region log in
        public ViewResult LogIn()
        {
            ViewData["return"] = Request.QueryString.Get("ReturnUrl");
            return View();
        }

        [HttpPost]
        public ActionResult LogIn(VMAccountLogIn model)
        {
            var result = accountService.AuthenticationUser(model.UserName, model.Password);
            if (result)
            {
                var a = Request;
                ViewData["validationResult"] = true;
                accountService.LogIn(model.UserName, model.RememberMe);
                string url = accountService.GetRedirectUrl(model.UserName, model.RememberMe);
                if(!string.IsNullOrEmpty(url))
                    return JavaScript(string.Format("top.location = \"{0}\" ", url));
                else
                    return JavaScript(string.Format("top.location = \"{0}\" ", string.Format(@"/{0}/view/index", CultureProvider.GetCurrentCultureString())));
            }
            else
            {
                ViewData["validationResult"] = false;
                this.isLogFailed = true;
            }
            return View("LogInControl");
        }

        #endregion

        #region log out
        public ActionResult LogOut()
        {
            accountService.LogOut();
            Response.Redirect(string.Format(@"/{0}/view/index", CultureProvider.GetCurrentCultureString()));
            return null;
        }

        #endregion

        public ActionResult Register()
        {
            ViewData["return"] = Request.QueryString.Get("ReturnUrl");
            return View();
        }

        [HttpPost]
        public ActionResult Register(VMAccountRegister registerData)
        {
            MembershipCreateStatus status;
            accountService.CreateUser(registerData.UserName, registerData.Password, registerData.Email, out status);
            if (status == MembershipCreateStatus.Success)
            {
                accountService.LogIn(registerData.UserName, false);
                string url = accountService.GetRedirectUrl(registerData.UserName, false);
                if (!string.IsNullOrEmpty(url))
                    return JavaScript(string.Format("top.location = \"{0}\" ", url));
                else
                    //TODO:if url is changed don't forget the hard code here
                    return JavaScript(string.Format("top.location = \"{0}\" ", string.Format(@"/{0}/view/index", CultureProvider.GetCurrentCultureString())));
            }
            else
            {
                ViewData["register_failed_message"] = status.ToString(); 
            }
            return View("RegisterControl");
        }

    }
}
